<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Shop.Classes.Model
 * @package   PsiFramework.Plugins.Shop.Classes.Model
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\Model\Order.html
 */

namespace PsiFramework\Plugins\Shop\Classes\Model;

use PsiFramework\Applications\DefaultApp\Classes\Model\User;

import("Classes.Model.Object");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Plugins\Shop\Classes\Model\Currency;

/**
 * The Order Object
 *
 * This is the class for the Order Object
 *
 * @category PsiFramework.Plugins.Shop.Classes.Model
 * @package  PsiFramework.Plugins.Shop.Classes.Model
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\Model\Order.html
 */
class Order extends Object
{

    /**
     * @var int
     */
    protected /*int*/             $_id ;
    /**
     * @var string
     */
    protected /*string*/          $_number ;
    /**
     * @var string
     */
    protected /*string*/          $_invoiceNumber ;
    /**
     * @var Date
     */
    protected /*Date*/            $_date ;
    /**
     * @var IUser
     */
    protected /*IUser*/           $_user ;
    /**
     * @var bool
     */
    protected /*bool*/            $_paid ;
    /**
     * @var double
     */
    protected /*double*/          $_discount ;
    /**
     * @var double
     */
    protected /*double*/          $_total ;
    /**
     * @var Currency
     */
    protected /*Currency*/        $_currency ;
    /**
     * @var int
     */
    protected /*int*/             $_status ;
    /**
     * @var string
     */
    protected /*string*/          $_meanOfPayment ;
    /**
     * @var string
     */
    protected /*string*/          $_customerName ;
    /**
     * @var string
     */
    protected /*string*/          $_customerAddress ;
    /**
     * @var string
     */
    protected /*string*/          $_customerZip ;
    /**
     * @var string
     */
    protected /*string*/          $_customerCity ;
    /**
     * @var OrderItemLink[]
     */
    protected /*OrderItemLink[]*/ $_items ;
    
    
    public static /*array*/ $incomplete               = array('code'=> '0', 'description'=>             'INCOMPLETE_OR_INVALID'); //Incomplete or invalid
    public static /*array*/ $cancelled                = array('code'=> '1', 'description'=>               'CANCELLED_BY_CLIENT'); //Cancelled by client
    public static /*array*/ $authRefused              = array('code'=> '2', 'description'=>             'AUTHORIZATION_REFUSED'); //Authorization refused
    public static /*array*/ $orderStored              = array('code'=> '4', 'description'=>                      'ORDER_STORED'); //Order stored
    public static /*array*/ $storedWaiting            = array('code'=>'40', 'description'=>    'STORED_WAITING_EXTERNAL_RESULT'); //Stored waiting external result
    public static /*array*/ $waitingPayment           = array('code'=>'41', 'description'=>            'WAITING_CLIENT_PAYMENT'); //Waiting client payment
    public static /*array*/ $authorized               = array('code'=> '5', 'description'=>                        'AUTHORIZED'); //Authorized
    public static /*array*/ $waitingExternal          = array('code'=>'50', 'description'=>'AUTHORIZED_WAITING_EXTERNAL_RESULT'); //Authorized waiting external result
    public static /*array*/ $authWaiting              = array('code'=>'51', 'description'=>             'AUTHORIZATION_WAITING'); //Authorization waiting
    public static /*array*/ $authNotKnown             = array('code'=>'52', 'description'=>           'AUTHORIZATION_NOT_KNOWN'); //Authorization not known
    public static /*array*/ $standBy                  = array('code'=>'55', 'description'=>                          'STAND_BY'); //Stand-by
    public static /*array*/ $okScheduledPayment       = array('code'=>'56', 'description'=>        'OK_WITH_SCHEDULED_PAYMENTS'); //OK with scheduled payments
    public static /*array*/ $errScheduledPayment      = array('code'=>'57', 'description'=>       'ERROR_IN_SCHEDULED_PAYMENTS'); //Error in scheduled payments
    public static /*array*/ $authManually             = array('code'=>'59', 'description'=>     'AUTHORIZATION_TO_GET_MANUALLY'); //Authoriz. to get manually
    public static /*array*/ $authCancelled            = array('code'=> '6', 'description'=>          'AUTHORIZED_AND_CANCELLED'); //Authorized and cancelled
    public static /*array*/ $authDeletionWaiting      = array('code'=>'61', 'description'=>    'AUTHORIZATION_DELETION_WAITING'); //Author. deletion waiting
    public static /*array*/ $authDeletionUncertain    = array('code'=>'62', 'description'=>  'AUTHORIZATION_DELETION_UNCERTAIN'); //Author. deletion uncertain
    public static /*array*/ $authDeletionRefused      = array('code'=>'63', 'description'=>    'AUTHORIZATION_DELETION_REFUSED'); //Author. deletion refused
    public static /*array*/ $authCancelledBis         = array('code'=>'64', 'description'=>          'AUTHORIZED_AND_CANCELLED'); //Authorized and cancelled
    public static /*array*/ $paymentDeleted           = array('code'=> '7', 'description'=>                   'PAYMENT_DELETED'); //Payment deleted
    public static /*array*/ $paymentDeletionPending   = array('code'=>'71', 'description'=>          'PAYMENT_DELETION_PENDING'); //Payment deletion pending
    public static /*array*/ $paymentDeletionUncertain = array('code'=>'72', 'description'=>        'PAYMENT_DELETION_UNCERTAIN'); //Payment deletion uncertain
    public static /*array*/ $paymentDeletionRefused   = array('code'=>'73', 'description'=>          'PAYMENT_DELETION_REFUSED'); //Payment deletion refused
    public static /*array*/ $paymentDeletedBis        = array('code'=>'74', 'description'=>                   'PAYMENT_DELETED'); //Payment deleted
    public static /*array*/ $deletionProcessedMerch   = array('code'=>'75', 'description'=>    'DELETION_PROCESSED_BY_MERCHANT'); //Deletion processed by merchant
    public static /*array*/ $refund                   = array('code'=> '8', 'description'=>                            'REFUND'); //Refund
    public static /*array*/ $refundPending            = array('code'=>'81', 'description'=>                    'REFUND_PENDING'); //Refund pending
    public static /*array*/ $refundUncertain          = array('code'=>'82', 'description'=>                  'REFUND_UNCERTAIN'); //Refund uncertain
    public static /*array*/ $refundRefused            = array('code'=>'83', 'description'=>                    'REFUND_REFUSED'); //Refund refused
    public static /*array*/ $paymentDeclined          = array('code'=>'84', 'description'=>  'PAYMENT_DECLINED_BY_THE_ACQUIRER'); //Payment declined by the acquirer
    public static /*array*/ $refundProcessed          = array('code'=>'85', 'description'=>      'REFUND_PROCESSED_BY_MERCHANT'); //Refund processed by merchant
    public static /*array*/ $paymentRequested         = array('code'=> '9', 'description'=>                   'PAYMENT_REQUEST'); //Payment requested
    public static /*array*/ $paymentProcessing        = array('code'=>'91', 'description'=>                'PAYMENT_PROCESSING'); //Payment processing
    public static /*array*/ $paymentuncertain         = array('code'=>'92', 'description'=>                 'PAYMENT_UNCERTAIN'); //Payment uncertain
    public static /*array*/ $paymentRefused           = array('code'=>'93', 'description'=>                   'PAYMENT_REFUSED'); //Payment refused
    public static /*array*/ $refundDeclined           = array('code'=>'94', 'description'=>   'REFUND_DECLINED_BY_THE_ACQUIRER'); //Refund declined by the acquirer
    public static /*array*/ $paymentProcessed         = array('code'=>'95', 'description'=>     'PAYMENT_PROCESSED_BY_MERCHANT'); //Payment processed by merchant
    public static /*array*/ $beingProcessed           = array('code'=>'99', 'description'=>                   'BEING_PROCESSED'); //Being processed
    
    /**
     * Constructor
     * 
     * @param int             $id              The id
     * @param string          $number          The number
     * @param Date            $date            The date
     * @param IUser           $user            The user
     * @param bool            $paid            Is the order paid ?
     * @param double          $discount        The total discount
     * @param double          $total           The total
     * @param Currency        $currency        The currency
     * @param int             $status          The status of the order
     * @param string          $meanOfPayment   The mean of payment
     * @param string          $customerName    The customer name
     * @param string          $customerAddress The customer address
     * @param string          $customerZip     The customer zip code
     * @param string          $customerCity    The customer city
     * @param OrderItemLink[] $items           The items
     * 
     * @return Order
     */
    public function __construct(
        /*int*/             $id=null,
        /*string*/          $number=null,
        /*string*/          $invoiceNumber=null,
        /*Date*/            $date=null,
        /*IUser*/           $user=null,
        /*bool*/            $paid=false,
        /*double*/          $discount=null,
        /*double*/          $total=null,
        /*Currency*/        $currency=null,
        /*int*/             $status=null,
        /*string*/          $meanOfPayment=null,
        /*string*/          $customerName=null,
        /*string*/          $customerAddress=null,
        /*string*/          $customerZip=null,
        /*string*/          $customerCity=null,
        /*OrderItemLink[]*/ $items=array()
    )
    {
        $this->_id = $id ;
        $this->_number = $number ;
        $this->_invoiceNumber = $invoiceNumber ;
        $this->_date = $date ;
        $this->_user = $user ;
        $this->_paid = $paid ;
        $this->_discount = $discount;
        $this->_total = $total;
        $this->_currency = $currency;
        $this->_status = $status;
        $this->_meanOfPayment = $meanOfPayment;
        $this->_customerName = $customerName;
        $this->_customerAddress = $customerAddress;
        $this->_customerZip = $customerZip;
        $this->_customerCity = $customerCity;
        $this->_items = $items ;
    }

    /**
     * Getter for the id / primary key
     *
     * @see Object::getId()
     *
     * @return int
     */
    public function getId()
    {
        return $this->_id ;
    }

    /**
     * Setter for the id
     *
     * @param int $id The id
     *
     * @return void
     */
    public function setId(/*int*/ $id)
    {
        $this->_id = $id ;
    }
    
    /**
     * Getter for the number
     *
     * @return string
     */
    public function getNumber()
    {
    	return $this->_number ;
    }
    
    /**
     * Setter for the number
     *
     * @param string $number The number
     *
     * @return void
     */
    public function setNumber(/*string*/ $number)
    {
    	$this->_number = $number ;
    }
    
    /**
     * Getter for the invoice number
     *
     * @return string
     */
    public function getInvoiceNumber()
    {
        return $this->_invoiceNumber ;
    }
    
    /**
     * Setter for the invoice number
     *
     * @param string $invoiceNumber The invoice number
     *
     * @return void
     */
    public function setInvoiceNumber(/*string*/ $invoiceNumber)
    {
        $this->_invoiceNumber = $invoiceNumber ;
    }
    
    /**
     * Getter for the date
     *
     * @return Date
     */
    public function getDate()
    {
    	return $this->_date ;
    }
    
    /**
     * Setter for the date
     *
     * @param Date $date The date
     *
     * @return void
     */
    public function setDate(/*Date*/ $date)
    {
    	$this->_date = $date ;
    }
    
    /**
     * Getter for the user
     *
     * @return User
     */
    public function getUser()
    {
    	return $this->_user ;
    }
    
    /**
     * Setter for the user
     *
     * @param IUser $user The user
     *
     * @return void
     */
    public function setUser(/*IUser*/ $user)
    {
    	$this->_user = $user ;
    }
    
    /**
     * Is the order paid ?
     *
     * @return bool
     */
    public function isPaid()
    {
    	return $this->_paid ;
    }
    
    /**
     * Setter for paid
     *
     * @param bool $paid paid ?
     *
     * @return void
     */
    public function setPaid(/*bool*/ $paid)
    {
    	$this->_paid = $paid ;
    }
    
    /**
     * Getter for the discount
     *
     * @return double
     */
    public function getDiscount()
    {
    	return $this->_discount ;
    }
    
    /**
     * Setter for the discount
     *
     * @param double $discount The discount
     *
     * @return double
     */
    public function setDiscount(/*double*/ $discount)
    {
    	$this->_discount = $discount ;
    }
    
    /**
     * Getter for the total
     *
     * @return double
     */
    public function getTotal()
    {
    	return $this->_total ;
    }
    
    /**
     * Setter for the total
     *
     * @param double $total The total
     *
     * @return double
     */
    public function setTotal(/*double*/ $total)
    {
    	$this->_total = $total ;
    }
    
    /**
     * Getter for the currency
     *
     * @return Currency
     */
    public function getCurrency()
    {
    	return $this->_currency ;
    }
    
    /**
     * Setter for the currency
     *
     * @param Currency $currency The currency
     *
     * @return Currency
     */
    public function setCurrency(/*Currency*/ $currency)
    {
    	$this->_currency = $currency ;
    }
    
    /**
     * Getter for the status
     *
     * @return int
     */
    public function getStatus()
    {
    	return $this->_status ;
    }
    
    /**
     * Setter for the status
     *
     * @param int $status The status
     *
     * @return int
     */
    public function setStatus(/*int*/ $status)
    {
    	$this->_status = $status ;
    }
    
    /**
     * Getter for the mean of payment
     *
     * @return string
     */
    public function getMeanOfPayment()
    {
    	return $this->_meanOfPayment ;
    }
    
    /**
     * Setter for the mean of payment
     *
     * @param string $meanOfPayment The mean of payment
     *
     * @return void
     */
    public function setMeanOfPayment(/*string*/ $meanOfPayment)
    {
    	$this->_meanOfPayment = $meanOfPayment ;
    }
    
    /**
     * Getter for the customer name
     *
     * @return string
     */
    public function getCustomerName()
    {
    	return $this->_customerName ;
    }
    
    /**
     * Setter for the customer name
     *
     * @param string $customerName The customer name
     *
     * @return void
     */
    public function setCustomerName(/*string*/ $customerName)
    {
    	$this->_customerName = $customerName ;
    }
    
    /**
     * Getter for the customer address
     *
     * @return string
     */
    public function getCustomerAddress()
    {
    	return $this->_customerAddress ;
    }
    
    /**
     * Setter for the customer address
     *
     * @param string $customerAddress The customer address
     *
     * @return void
     */
    public function setCustomerAddress(/*string*/ $customerAddress)
    {
    	$this->_customerAddress = $customerAddress ;
    }
    
    /**
     * Getter for the customer zip code
     *
     * @return string
     */
    public function getCustomerZip()
    {
    	return $this->_customerZip ;
    }
    
    /**
     * Setter for the customer zip code
     *
     * @param string $customerZip The customer zip code
     *
     * @return void
     */
    public function setCustomerZip(/*string*/ $customerZip)
    {
    	$this->_customerZip = $customerZip ;
    }
    
    /**
     * Getter for the customer city
     *
     * @return string
     */
    public function getCustomerCity()
    {
    	return $this->_customerCity ;
    }
    
    /**
     * Setter for the customer city
     *
     * @param string $customerCity The customer city
     *
     * @return void
     */
    public function setCustomerCity(/*string*/ $customerCity)
    {
    	$this->_customerCity = $customerCity ;
    }
    
    
    /**
     * Getter for the items
     *
     * @return OrderItemLink[]
     */
    public function getItems()
    {
    	return $this->_items ;
    }
    
    /**
     * Setter for the items
     *
     * @param OrderItemLink[] $items the items
     *
     * @return void
     */
    public function setItems(/*OrderItemLink[]*/ $items)
    {
    	$this->_items = $items ;
    }
    
    /**
     * Add an item
     *
     * @param OrderItemLink $item the item
     *
     * @return void
     */
    public function addItem(/*OrderItemLink*/ $item)
    {
    	$this->_items[] = $item ;
    }

    /**
     * Get the status code information
     * 
     * @param string $statusCode the status code
     * 
     * @return array
     */
    public static function getStatusCodeInformation(/*string*/ $statusCode)
    {
        $codes
            = array(
        		'0' =>self::$incomplete,
        		'1' =>self::$cancelled,
        		'2' =>self::$authRefused,
        		'4' =>self::$orderStored,
        		'40'=>self::$storedWaiting,
        		'41'=>self::$waitingPayment,
        		'5' =>self::$authorized,
        		'50'=>self::$waitingExternal,
        		'51'=>self::$authWaiting,
        		'52'=>self::$authNotKnown,
        		'55'=>self::$standBy,
        		'56'=>self::$okScheduledPayment,
        		'57'=>self::$errScheduledPayment,
        		'59'=>self::$authManually,
        		'6' =>self::$authCancelled,
        		'61'=>self::$authDeletionWaiting,
        		'62'=>self::$authDeletionUncertain,
        		'63'=>self::$authDeletionRefused,
        		'64'=>self::$authCancelledBis,
        		'7' =>self::$paymentDeleted,
        		'71'=>self::$paymentDeletionPending,
        		'72'=>self::$paymentDeletionUncertain,
        		'73'=>self::$paymentDeletionRefused,
        		'74'=>self::$paymentDeletedBis,
        		'75'=>self::$deletionProcessedMerch,
        		'8' =>self::$refund,
        		'81'=>self::$refundPending,
        		'82'=>self::$refundUncertain,
        		'83'=>self::$refundRefused,
        		'84'=>self::$paymentDeclined,
        		'85'=>self::$refundProcessed,
        		'9' =>self::$paymentRequested,
        		'91'=>self::$paymentProcessing,
        		'92'=>self::$paymentuncertain,
        		'93'=>self::$paymentRefused,
        		'94'=>self::$refundDeclined,
        		'95'=>self::$paymentProcessed,
        		'99'=>self::$beingProcessed
        );
        
        if (array_key_exists($statusCode, $codes)) {
            return $codes[$statusCode];
        } else {
            return array();
        }
    }

    /**
     * Shows the Order as a string
     *
     * @see Object::toString()
     *
     * @return string
     */
    public function toString()
    {
        return $this->_number ;
    }

}
